package com.hspedu.hspliving.commodity.controller;

import java.util.Arrays;
import java.util.Map;

import com.hspedu.hspliving.commodity.vo.SpuSaveVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hspedu.hspliving.commodity.entity.SpuInfoEntity;
import com.hspedu.hspliving.commodity.service.SpuInfoService;
import com.hspedu.common.utils.PageUtils;
import com.hspedu.common.utils.R;



/**
 * 商品spu 信息
 *
 * @author bing
 * @email bing@gmail.com
 * @date 2024-07-23 20:58:32
 */
@RestController
@RequestMapping("commodity/spuinfo")
public class SpuInfoController {
    @Autowired
    private SpuInfoService spuInfoService;

    /**
     * spu商品上架
     * @param spuId
     * @return
     */
    @RequestMapping("/{spuId}/up")
    public R spuUp(@PathVariable("spuId") Long spuId){
        spuInfoService.up(spuId);
        return R.ok();
    }

    /**
     * spu商品下架
     * @param spuId
     * @return
     */
    @RequestMapping("/{spuId}/down")
    public R spuDown(@PathVariable("spuId") Long spuId){
        spuInfoService.down(spuId);
        return R.ok();
    }

    /**
     * 1. 因为保存商品信息，涉及到的表非常多，不是一个SpuInfoEntity 实体类能包括的.
     * 2. 将SpuInfoEntity 改成我们前面生成的SpuSaveVo 包含了json 提交所有信息
     * 3. 我们写一个定制的方法saveSpuInfo 来完成
     * @param spuSaveVO
     * @return
     */
    @RequestMapping("/save")
    public R saveSpuInfo(@RequestBody SpuSaveVO spuSaveVO){
        spuInfoService.saveSpuInfo(spuSaveVO);
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        //PageUtils page = spuInfoService.queryPage(params);//不带检索条件按
        PageUtils page = spuInfoService.queryPageByCondition(params); //带检索条件
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		SpuInfoEntity spuInfo = spuInfoService.getById(id);

        return R.ok().put("spuInfo", spuInfo);
    }

//    /**
//     * 保存
//     */
//    @RequestMapping("/save")
//    public R save(@RequestBody SpuInfoEntity spuInfo){
//		spuInfoService.save(spuInfo);
//
//        return R.ok();
//    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody SpuInfoEntity spuInfo){
		spuInfoService.updateById(spuInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		spuInfoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
